<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>二进制转换为十进制</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.bindec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">bindec</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">bindec</span> &mdash; <span class="dc-title">二进制转换为十进制</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.bindec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>bindec</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$binary_string</code></span>
   ) : <span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span></div>

  <p class="para rdfs-comment">
   返回
   <code class="parameter">binary_string</code> 参数所表示的二进制数的十进制等价值。
  </p>
  <p class="para">
   <span class="function"><strong>bindec()</strong></span> 将一个二进制数转换成
   <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>，或者出于大小的需要，转换为 <span class="type"><a href="language.types.float.html" class="type float">float</a></span> 类型。
  </p>
  <p class="para">
   <span class="function"><strong>bindec()</strong></span> 将所有的
   <code class="parameter">binary_string</code> 值解释为无符号整数。这是由于
   <span class="function"><strong>bindec()</strong></span> 函数将其最高有效位视为数量级而非符号位。
  </p>
</div>

 <div class="refsect1 parameters" id="refsect1-function.bindec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">binary_string</code></dt>

     <dd>

      <p class="para">
       要转换的二进制字符串
      </p>
     </dd>

    
   </dl>

  </p>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    参数必须为字符串。使用其他数据类型会导致不可预知的结果。
   </p>
  </div>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.bindec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <code class="parameter">binary_string</code> 的十进制数值
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.bindec-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.1.0</td>
       <td>
        本函数如今可以转换超过程序运行平台中
        <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 类型最大值的数，此时其值会返回为
        <span class="type"><a href="language.types.float.html" class="type float">float</a></span> 类型。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.bindec-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4117">
    <p><strong>Example #1 <span class="function"><strong>bindec()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'110011'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'000110011'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #DD0000">'111'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
51
51
7
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4118">
    <p><strong>Example #2 <span class="function"><strong>bindec()</strong></span> 将输入解读为无符号整数</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;The&nbsp;lesson&nbsp;from&nbsp;this&nbsp;example&nbsp;is&nbsp;in&nbsp;the&nbsp;output<br />&nbsp;*&nbsp;rather&nbsp;than&nbsp;the&nbsp;PHP&nbsp;code&nbsp;itself.<br />&nbsp;*/<br /><br /></span><span style="color: #0000BB">$magnitude_lower&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pow</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$magnitude_lower&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$magnitude_lower</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'See&nbsp;the&nbsp;rollover?&nbsp;&nbsp;Watch&nbsp;it&nbsp;next&nbsp;time&nbsp;around...'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_INT_MAX</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP_INT_MAX'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(~</span><span style="color: #0000BB">PHP_INT_MAX</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;one&nbsp;more&nbsp;than&nbsp;PHP_INT_MAX'</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;the&nbsp;largest&nbsp;unsigned&nbsp;integer'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'interpreted&nbsp;to&nbsp;be&nbsp;the&nbsp;largest&nbsp;unsigned&nbsp;integer<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(18446744073709551615)&nbsp;but&nbsp;skewed&nbsp;by&nbsp;float&nbsp;precision'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">p</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$note</span><span style="color: #007700">);<br /><br /><br />function&nbsp;</span><span style="color: #0000BB">p</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$note&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"input:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'%0'&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">PHP_INT_SIZE&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bin&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"binary:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bin</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'precision'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;For&nbsp;readability&nbsp;on&nbsp;64&nbsp;bit&nbsp;boxes.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dec&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">bindec</span><span style="color: #007700">(</span><span style="color: #0000BB">$bin</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'bindec():&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$dec&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$note</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"NOTE:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$note</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程在 32 位机器上的输出:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
input:        1073741823
binary:       00111111111111111111111111111111
bindec():     1073741823

input:        1073741824
binary:       01000000000000000000000000000000
bindec():     1073741824
NOTE:         See the rollover?  Watch it next time around...

input:        2147483647
binary:       01111111111111111111111111111111
bindec():     2147483647
NOTE:         PHP_INT_MAX

input:        -2147483648
binary:       10000000000000000000000000000000
bindec():     2147483648
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       11111111111111111111111111111111
bindec():     4294967295
NOTE:         interpreted to be the largest unsigned integer
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程在 64 位机器上的输出:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
input:        4611686018427387903
binary:       0011111111111111111111111111111111111111111111111111111111111111
bindec():     4611686018427387903

input:        4611686018427387904
binary:       0100000000000000000000000000000000000000000000000000000000000000
bindec():     4611686018427387904
NOTE:         See the rollover?  Watch it next time around...

input:        9223372036854775807
binary:       0111111111111111111111111111111111111111111111111111111111111111
bindec():     9223372036854775807
NOTE:         PHP_INT_MAX

input:        -9223372036854775808
binary:       1000000000000000000000000000000000000000000000000000000000000000
bindec():     9223372036854775808
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       1111111111111111111111111111111111111111111111111111111111111111
bindec():     18446744073709551616
NOTE:         interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.bindec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="decbin.html" class="function" rel="rdfs-seeAlso">decbin()</a> - 十进制转换为二进制</span></li>
    <li class="member"><span class="function"><a href="octdec.html" class="function" rel="rdfs-seeAlso">octdec()</a> - 八进制转换为十进制</span></li>
    <li class="member"><span class="function"><a href="hexdec.html" class="function" rel="rdfs-seeAlso">hexdec()</a> - 十六进制转换为十进制</span></li>
    <li class="member"><span class="function"><a href="base_convert.html" class="function" rel="rdfs-seeAlso">base_convert()</a> - 在任意进制之间转换数字</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>